{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  \n",
    "+ 学号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "举头望无尽灰云\n",
      "那季节叫做寂寞\n",
      "背包塞满了家用\n",
      "路就这样开始走\n",
      "日不见太阳的暖\n",
      "夜不见月光的蓝\n",
      "不得不选择寒冷的开始\n",
      "留下只拥有遗憾\n",
      "命运的安排\n",
      "遵守自然的逻辑\n",
      "谁都无法揭谜底\n",
      "\n",
      "喔～远离家乡不甚唏嘘\n",
      "幻化成秋夜\n",
      "而我却像落叶归根\n",
      "坠在你心间\n",
      "几分忧郁几分孤单\n",
      "都心甘情愿\n",
      "我的爱像落叶归根\n",
      "家唯独在你身边\n"
     ]
    }
   ],
   "source": [
    "print('举头望无尽灰云\\n那季节叫做寂寞\\n背包塞满了家用\\n路就这样开始走\\n日不见太阳的暖\\n夜不见月光的蓝\\n不得不选择寒冷的开始\\n留下只拥有遗憾\\n命运的安排\\n遵守自然的逻辑\\n谁都无法揭谜底')\n",
    "\n",
    "def fu_ge():\n",
    "    print('\\n喔～远离家乡不甚唏嘘\\n幻化成秋夜\\n而我却像落叶归根\\n坠在你心间\\n几分忧郁几分孤单\\n都心甘情愿\\n我的爱像落叶归根\\n家唯独在你身边')\n",
    "fu_ge()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值是82.76923076923077\n",
      "n=13\n",
      "样本方差是 22090.192308\n",
      "\n",
      "\n",
      "均值是7.0\n",
      "n=13\n",
      "样本方差是 15.166667\n"
     ]
    }
   ],
   "source": [
    "def statistics_value(list_number):\n",
    "    \n",
    "    m=0\n",
    "    for w in list_number:\n",
    "        m=w+m\n",
    "    \n",
    "    mean=m/len(list_number)\n",
    "    print('均值是'+ str(mean))\n",
    "    n = len (list_number)\n",
    "    print('n=%d' % n)\n",
    "    sum=0\n",
    "    for a in list_number:\n",
    "       sum = (a - mean)**2 + sum\n",
    "    \n",
    "    variance = sum/(n -1)\n",
    "    print('样本方差是 %f'%variance)\n",
    "    \n",
    "     \n",
    "    \n",
    "list_1=[11,23,34,556,67,88,123,2,3,4,12,121,32]    \n",
    "statistics_value(list_1)\n",
    "print('\\n')\n",
    "list_1 = [1,2,3,4,5,6,7,8,9,10,11,12,13]\n",
    "statistics_value(list_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值是82.76923076923077\n",
      "n=13\n",
      "样本方差是 22090.192308\n",
      "\n",
      "\n",
      "均值是7.0\n",
      "n=13\n",
      "样本方差是 15.166667\n",
      "方差是 14.000000\n",
      "样本标准差是 3.894440\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def statistics_value(list_number,stan_var = None,var = None):\n",
    "    \n",
    "    m=0\n",
    "    for w in list_number:\n",
    "        m=w+m\n",
    "    \n",
    "    mean=m/len(list_number)\n",
    "    print('均值是'+ str(mean))\n",
    "    n = len (list_number)\n",
    "    print('n=%d' % n)\n",
    "    sum=0\n",
    "    for a in list_number:\n",
    "       sum = (a - mean)**2 + sum\n",
    "    \n",
    "    variance = sum/(n -1)\n",
    "    print('样本方差是 %f'%variance)\n",
    "    \n",
    "    if var :\n",
    "        var = sum/n\n",
    "        print('方差是 %f'% var)\n",
    "    \n",
    "    if stan_var :\n",
    "        stan_var= math.sqrt(variance)\n",
    "        print('样本标准差是 %f' % stan_var)\n",
    "list_1=[11,23,34,556,67,88,123,2,3,4,12,121,32]    \n",
    "statistics_value(list_1)\n",
    "print('\\n')\n",
    "list_1 = [1,2,3,4,5,6,7,8,9,10,11,12,13]\n",
    "statistics_value(list_1,stan_var=1,var=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值是82.76923076923077\n",
      "n=13\n",
      "样本方差是 22090.192308\n",
      "样本标准差是 148.627697\n",
      "mini= 2.000000\n",
      "\n",
      "\n",
      "均值是7.0\n",
      "n=13\n",
      "样本方差是 15.166667\n",
      "方差是 14.000000\n",
      "标准误是 0.000000\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def statistics_value(list_number,stan_var = None,var = None,maxi=None,mini=None,stand_err=None):\n",
    "    \n",
    "    m=0\n",
    "    for w in list_number:\n",
    "        m=w+m\n",
    "    \n",
    "    mean=m/len(list_number)\n",
    "    print('均值是'+ str(mean))\n",
    "    n = len (list_number)\n",
    "    print('n=%d' % n)\n",
    "    sum=0\n",
    "    for a in list_number:\n",
    "       sum = (a - mean)**2 + sum\n",
    "    \n",
    "    variance = sum/(n -1)\n",
    "    print('样本方差是 %f'%variance)\n",
    "    \n",
    "    if var :\n",
    "        var = sum/n\n",
    "        print('方差是 %f'% var)\n",
    "    \n",
    "    if stan_var :\n",
    "        stan_var= math.sqrt(variance)\n",
    "        print('样本标准差是 %f' % stan_var)\n",
    "   \n",
    "    if maxi:\n",
    "        maxi = list_number[0]\n",
    "        for value in list_number:\n",
    "            if value > maxi:\n",
    "                maxi = value\n",
    "        print ('maxi= %f' % maxi)\n",
    "    if mini:\n",
    "        mini = list_number[0]\n",
    "        for value in list_number:\n",
    "            if value < mini:\n",
    "                mini = value\n",
    "        print('mini= %f' % mini)\n",
    "    if stand_err:\n",
    "        stand_err = stan_var/math.sqrt(n)\n",
    "        print('标准误是 %f' % stand_err)\n",
    "list_1=[11,23,34,556,67,88,123,2,3,4,12,121,32]    \n",
    "statistics_value(list_1,mini=1,maxi=0,stan_var=1)\n",
    "print('\\n')\n",
    "list_1 = [1,2,3,4,5,6,7,8,9,10,11,12,13]\n",
    "statistics_value(list_1,stan_var=0,var=1,stand_err=1)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
